package com.cxps.server.inventory.controller;

import com.cxps.cloud.message.ApiResponse;
import com.cxps.server.inventory.domain.InventoryInfoQuery;
import com.cxps.server.inventory.domain.InventoryWarning;
import com.cxps.server.inventory.service.InventoryInfoService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 * 商品库存信息表 前端控制器
 * </p>
 *
 * @author YouCai.Liu
 * @since 2021-11-02
 */
@RestController
@RequestMapping("/inventory")
@Api(tags = "库存信息API")
public class InventoryInfoController {

    @Autowired
    private InventoryInfoService inventoryInfoService;

    /**
     * 商品库存信息列表
     *
     * @return
     */
    @PostMapping("/list")
    @ApiOperation(value = "商品库存信息列表", response = ApiResponse.class)
    public ApiResponse list(@RequestBody InventoryInfoQuery query) {
        return ApiResponse.fireSuccess(inventoryInfoService.list(query));
    }

    /**
     * 商品库存预警设置
     *
     * @param warning
     * @return
     */
    @PostMapping("/warning")
    @ApiOperation(value = "商品库存预警设置", response = ApiResponse.class)
    public ApiResponse warning(@RequestBody InventoryWarning warning) throws IllegalAccessException {
        try {
            inventoryInfoService.warning(warning);
            return ApiResponse.fireSuccess();
        } catch (IllegalArgumentException e) {
            return ApiResponse.fireError(e.getMessage());
        }
    }

    /**
     * 商品库存流水列表
     *
     * @return
     */
    @PostMapping("/flowing/list")
    @ApiOperation(value = "商品库存流水列表", response = ApiResponse.class)
    public ApiResponse flowingList(@RequestBody InventoryInfoQuery query) {
        return ApiResponse.fireSuccess(inventoryInfoService.flowingList(query));
    }

    /**
     * 根据商品ID查询商品入库流水
     *
     * @param productId
     * @return
     */
    @PostMapping("/runWater")
    @ApiOperation(value = "商品入库流水", response = ApiResponse.class)
    public ApiResponse runningWater(@RequestParam String productId) {
        return ApiResponse.fireSuccess(inventoryInfoService.runningWater(productId));
    }


    /**
     * 入库设置
     *
     * @return
     */
    @GetMapping("/warning")
    @ApiOperation(value = "商品入库设置", response = ApiResponse.class)
    public ApiResponse add(@RequestParam String productId, @RequestParam Integer kcNum) {
        inventoryInfoService.add(productId, kcNum);
        return ApiResponse.fireSuccess();
    }

}

